package com.paw.security.jwt.controller;

import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lvzihai
 * @date 2021/7/13
 **/
@RestController
public class SecurityJwtController {


  @GetMapping(value = {"", "welcome"})
  public String welcome() {
    return "Welcome!!!";
  }

  @GetMapping("index")
  public String index() {
    return "index!!!";
  }

  @GetMapping("admin")
  public String admin() {
    return "admin!!!";
  }

  @GetMapping("user")
  public String user() {
    return "user!!!";
  }

  @GetMapping("customer")
  public String customer() {
    return "customer!!!";
  }

  /**
   * 方法执行前鉴权
   *
   * @return
   */
  @GetMapping("roleAdmin")
  @Secured("ROLE_ADMIN")
  public String roleAdmin() {
    return "roleAdmin!!!";
  }

  /**
   * 方法执行前鉴权
   *
   * @return
   */
  @GetMapping("preAuthorize")
  @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
  public String preAuthorize() {
    System.out.println("preAuthorize…………");
    return "preAuthorize!!!";
  }

  /**
   * 方法执行完再鉴权
   *
   * @return
   */
  @GetMapping("postAuthorize")
  @PostAuthorize("hasAnyRole('ROLE_USER')")
  public String postAuthorize() {
    System.out.println("postAuthorize…………");
    return "PostAuthorize!!!";
  }

  @GetMapping("/pub/a")
  public String puba(){
    return "/pub/puba";
  }

}
